package com.zmn.oms.admin.controller.work.modify;

import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.common.annotation.PageRequestCas;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.work.modify.must.OrderDistributeBackDTO;
import com.zmn.oms.model.vo.work.OrderDistributeBackVO;
import com.zmn.oms.zmn.business.interfaces.order.OrderVerifyBService;
import com.zmn.oms.zmn.business.interfaces.work.OrderWorkDistributeBackBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;


/**
 * 类描述：派单撤回
 * @author lujia
 * @date 2019/5/22 11:19
 */
@Controller
@RequestMapping("/work/modify")
public class WorkDistributeBackController extends WorkBaseController {

    @Autowired
    private OrderWorkDistributeBackBService orderWorkDistributeBackBService;
    @Autowired
    private OrderVerifyBService orderVerifyBService;

    /**
     * 派单撤回
     */
    @RequestMapping(value = "/distribute/back")
    @PageRequestCas(type = OrderConsts.ORDER_OP_TYPE_DISTRIBUTE_BACK)
    public ModelAndView distributeBack(Long orderId, Long workId) {

        ModelAndView mv = new ModelAndView("work/modify/distributeback");

        OrderDistributeBackVO distributeBackVO = orderWorkDistributeBackBService.getDistributeBackVO(orderId, workId);

        String tips = orderVerifyBService.getAmountTips(orderId, workId);

        mv.addObject("tips", tips);

        mv.addObject("distributeBackVO", distributeBackVO);
        return mv;
    }

    /**
     * 保存派单撤回
     */
    @RequestMapping(value = "/save/distribute/back")
    @ResponseBody
    public ResultDTO saveDistributeBack(@RequestBody @Valid OrderDistributeBackDTO orderDistributeBackDTO) {
        try {
            orderDistributeBackDTO.setStatus(OrderStatusConsts.WORK_STATUS_ASSIGN);
            orderDistributeBackDTO.setResultStatus(OrderStatusConsts.WORK_RESULT_DOING);
            orderWorkDistributeBackBService.saveDistributeBack(orderDistributeBackDTO);
            return ResultDTO.success();
        } catch (OmsBaseException e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

}
